#!/bin/bash
# This script installs ffsb and runs the regression tests
###SHELLPACK preamble ffsb-install 6.0-rc2

DURATION=300
NUM_FILES=100
NUM_DIRECTORIES=100
NUM_THREADS=1
FILE_PROFILE=single-file-single-directory
FILESIZE_PROFILE=none
FILEWEIGHT_PROFILE=none
OPERATIONS_PROFILE=seq-read
MIN_FILESIZE=$((32*1048576))
MAX_FILESIZE=$((32*1048576))

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --duration           DURATION
###SHELLPACK parseargParam --file-profile       FILE_PROFILE
###SHELLPACK parseargParam --filesize-profile   FILESIZE_PROFILE
###SHELLPACK parseargParam --operations-profile OPERATIONS_PROFILE
###SHELLPACK parseargParam --files              NUM_FILES
###SHELLPACK parseargParam --directories        NUM_DIRECTORIES
###SHELLPACK parseargParam --threads            NUM_THREADS
###SHELLPACK parseargParam --min-filesize       MIN_FILESIZE
###SHELLPACK parseargParam --max-filesize       MAX_FILESIZE
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required ffsb-${VERSION}
###SHELLPACK init_complete

# Write config
CONFIG=$LOGDIR_RESULTS/ffsb.config
echo -n > $CONFIG

echo "time=$DURATION"				> $CONFIG
echo "alignio=1"				>> $CONFIG
echo						>> $CONFIG
echo "[filesystem0]"				>> $CONFIG
echo "	location=$SHELLPACK_DATA"		>> $CONFIG
case $FILE_PROFILE in
many-file-single-directory)
	echo "	num_files=$NUM_FILES"		>> $CONFIG
	;;
single-file-single-directory)
	;;
many-file-many-directory)
	echo "	num_files=$NUM_FILES"		>> $CONFIG
	echo "	num_dirs=$NUM_DIRECTORIES"	>> $CONFIG
	;;
*)
	die Unrecognised file profile: $FILE_PROFILE
esac
case $FILESIZE_PROFILE in
none)
	;;
min-max)
	echo "	min_filesize=$MIN_FILESIZE"	>> $CONFIG
	echo "	max_filesize=$MAX_FILESIZE"	>> $CONFIG
	echo "	reuse=1"			>> $CONFIG
	;;
weight-mailserver)
	echo "	size_weight 1024 10
	size_weight 2048 15
	size_weight 4096 16
	size_weight 8192 16
	size_weight 16384 15
	size_weight 32768 10
	size_weight 65536 8
	size_weight 131072 4
	size_weight 262144 3
	size_weight 524288 2
	size_weight 1048576 1"			>> $CONFIG
	;;
*)
	die Unrecognised filesize profile: $FILE_PROFILE
	;;
esac
echo "[end0]"					>> $CONFIG

echo						>> $CONFIG
echo "[threadgroup0]"				>> $CONFIG
echo "	num_threads=$NUM_THREADS"		>> $CONFIG
case $OPERATIONS_PROFILE in
seq-read)
	echo "	read_weight=1"			>> $CONFIG
	echo "	read_size=$MIN_FILESIZE"	>> $CONFIG
	echo "	read_blocksize=4096"		>> $CONFIG
	;;
random-read)
	echo "	read_random=1"			>> $CONFIG
	echo "	read_weight=1"			>> $CONFIG
	echo "	read_size=1048576"		>> $CONFIG
	echo "	read_blocksize=4096"		>> $CONFIG
	;;
random-write)
	echo "	write_random=1"			>> $CONFIG
	echo "	write_weight=1"			>> $CONFIG
	echo "	write_size=1048576"		>> $CONFIG
	echo "	write_blocksize=4096"		>> $CONFIG
	;;
file-create)
	echo "	create_weight=1"		>> $CONFIG
	echo "	write_blocksize=4096"		>> $CONFIG
	;;
mailserver)
	echo "	readall_weight=4
	create_weight=2
	delete_weight=1

	write_size=4096
	write_blocksize=4096

	read_size=4096
	read_blocksize=4096"			>> $CONFIG
echo 						>> $CONFIG
	;;
esac
echo "	[stats]
		enable_stats=1
		enable_range=1

		msec_range    0.00      0.01
		msec_range    0.01      0.02
		msec_range    0.02      0.05
		msec_range    0.05      0.10
		msec_range    0.10      0.20
		msec_range    0.20      0.50
		msec_range    0.50      1.00
		msec_range    1.00      2.00
		msec_range    2.00      5.00
		msec_range    5.00     10.00
		msec_range   10.00     20.00
		msec_range   20.00     50.00
		msec_range   50.00    100.00
		msec_range  100.00    200.00
		msec_range  200.00    500.00
		msec_range  500.00   1000.00
		msec_range 1000.00   2000.00
		msec_range 2000.00   5000.00
		msec_range 5000.00  10000.00
	[end]"					>> $CONFIG
echo "[end0]"				>> $CONFIG

monitor_pre_hook $LOGDIR_RESULTS $P
./bin/ffsb $CONFIG 2>&1 | tee $LOGDIR_RESULTS/ffsb.log \
				|| die Failed to run ffsb
monitor_post_hook $LOGDIR_RESULTS $P

exit $SHELLPACK_SUCCESS
